<template>
	<view style="width:100%;height:100%;display: flex;align-items: center;justify-content: center;">
		<view v-if="errMsg.length>0" style="font-size: 36upx;">{{errMsg}}</view>
	</view>
</template>

<script>
	var page;
	var params;
	export default {
		data() {
			return {
				errMsg: "",
			}
		},
		onLoad: function(options) {
			// https://sz.smates.cn/onlineRetailers/h5/index.html#/pages/main/main?userinfo=%7B%22data%22%3A%22%2Fi4Rt%2FkfmcjmxbjA1CJDDl9pMeC0JP5hGyDfdx4RTzl1Xf%2B9PZPWSrgOqvPC%2BjgF0ril5MnSstvFEWxvESo3V9vOoLdMe37Ia91niYGk%2FVxy9%2Fsw7iWfM8StxD%2BopJUwhepXf6ogy9EeIbc95pymzAtawUuH0RaBEnZUjm1%2FA0vDUE6Ch%2FV9U4xXkuWQZt7syGDHI6C28ROYyJTm4O%2BNdvvEGyCYYr7H6ZMUT%2FJyW8Crl1bEzRFIr8hTZDoSWG9XBIqBLqDYlRq7Euz4X5m%2FlJnX2bfHx59q6h1hWxxJt8et2%2FRPBysHfVRmGZb4JqRPwGls%2Fje7CudaUbahiTNXT0mdvXc5CUC%2FhmekJCJUDL%2FfwK24%2BaEHTXnJ1yHLuznbkia6brBUoPCMfDm0rkFaixyLBEWXaUyeoHRk8Hq1ZfOiABfwPQdB5GUjX%2FdJgy5jWNLHTl0DEubOcNKkvri3QMnbz7d3pcX3EnYVO5jveoZJNnwhbB3NGltaQ52BCIiVA%2Bx%2B2hPr%2FEK%2FBhOqZbfxpuS0j8oh2yfoU3uMO0Q7b8ZTZJSCkBo%2BY5sw5XGaAwKY5SpRcnbasfjlstKwS2VTDAKB9JkKd6VGpLYe%2FOEhXUmom%2Bb%2F%2BonYkMz8PI4TEB6HXXWu5CLjjtkQssLR2ZefPqbaw7ZhorQ5Px0oAQbJgWYS8fEE%2FB80KKJU91KE3ij%2Bp4Fu2%2FC7li6NPImulShlA5lJv4LgEj7%2BYbnAUgs89q%2BmY%2B2rRintHZQP%2FJaRas9CPv8I%2FQgalb%2BuHzFWR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3301Yemf59Hoh6L5WLYBfgJtu7XSJPUmKqi0Xw7nSMD2zwSyevl2hFGijGGj8NDL5ShNKawVs3w%2B0g%2F%2FHEzawxcU6bPUbwDQWbPqIqVUBBKCDbQUtKB3z0Pwc8C9ua2CXBIyWixadpZ3L0n%2BVuog%3D%3D%22%2C%22appid%22%3A%22wx1456c55ff4d695ba%22%2C%22secret%22%3A%225757602b2b9c54495193b3d7832fa5e3%22%7D&appid=wx1456c55ff4d695ba&openid=wx21fe321ff1e3e2e0&page=1
			// https://192.168.1.116/onlineRetailers/h5/index.html#/pages/main/main?userinfo=%7B%22data%22%3A%22%2Fi4Rt%2FkfmcjmxbjA1CJDDl9pMeC0JP5hGyDfdx4RTzl1Xf%2B9PZPWSrgOqvPC%2BjgF0ril5MnSstvFEWxvESo3V9vOoLdMe37Ia91niYGk%2FVxy9%2Fsw7iWfM8StxD%2BopJUwhepXf6ogy9EeIbc95pymzAtawUuH0RaBEnZUjm1%2FA0vDUE6Ch%2FV9U4xXkuWQZt7syGDHI6C28ROYyJTm4O%2BNdvvEGyCYYr7H6ZMUT%2FJyW8Crl1bEzRFIr8hTZDoSWG9XBIqBLqDYlRq7Euz4X5m%2FlJnX2bfHx59q6h1hWxxJt8et2%2FRPBysHfVRmGZb4JqRPwGls%2Fje7CudaUbahiTNXT0mdvXc5CUC%2FhmekJCJUDL%2FfwK24%2BaEHTXnJ1yHLuznbkia6brBUoPCMfDm0rkFaixyLBEWXaUyeoHRk8Hq1ZfOiABfwPQdB5GUjX%2FdJgy5jWNLHTl0DEubOcNKkvri3QMnbz7d3pcX3EnYVO5jveoZJNnwhbB3NGltaQ52BCIiVA%2Bx%2B2hPr%2FEK%2FBhOqZbfxpuS0j8oh2yfoU3uMO0Q7b8ZTZJSCkBo%2BY5sw5XGaAwKY5SpRcnbasfjlstKwS2VTDAKB9JkKd6VGpLYe%2FOEhXUmom%2Bb%2F%2BonYkMz8PI4TEB6HXXWu5CLjjtkQssLR2ZefPqbaw7ZhorQ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w0neU3nQTMjHKWnoPCnEKpSXn5LUKtaWbHU1hmv0FBxDYC4EyvrF9SqnpNgIcupf%2FMfnBt8u7dpp1cjsbg%2F%2FRVLp3am4K9BYsWsp9nReRlZ%2Ft7XuTfyZ3301Yemf59Hoh6L5WLYBfgJtu7XSJPUmKqi0Xw7nSMD2zwSyevl2hFGijGGj8NDL5ShNKawVs3w%2B0g%2F%2FHEzawxcU6bPUbwDQWbPqIqVUBBKCDbQUtKB3z0Pwc8C9ua2CXBIyWixadpZ3L0n%2BVuog%3D%3D%22%2C%22appid%22%3A%22wx1456c55ff4d695ba%22%2C%22secret%22%3A%225757602b2b9c54495193b3d7832fa5e3%22%7D&appid=wx1456c55ff4d695ba&openid=wx21fe321ff1e3e2e0&page=0
			// console.log("11111111111111111111111")
			// return
			var that = this;
			if (!options.appid || options.appid == "null" || options.appid == "") {
				this.errMsg = "参数错误，未设置appid！";
				return
			} else if ((!options.secret || options.appid == "null" || options.appid == "") && (!options.openid || options.openid ==
					"null" || options.openid == "") && (!options.openId || options.openId == "null" || options.openId == "")) {
				this.errMsg = "参数错误，未设置secret或openid！";
				return
			} else if (!options.userinfo || options.userinfo == "null" || options.userinfo == "") {
				this.errMsg = "参数错误，未设置userinfo";
				return
			}
			page = options.page || "0";
			if (options.param) {
				params = JSON.parse(decodeURIComponent(options.param));
			}

			let appid = options.appid;
			let openid = options.openid || options.openId;
			let secret = options.secret;

			this.pub.setAppid(appid)

			this.userinfo = JSON.parse(decodeURIComponent(options.userinfo));
			
			//如果存在openid,则不用再去自己获取openid
			if (openid && openid != "null" && openid != "") {
				this.pub.setOpenid(openid);
				this.pub.getEncryptionOid(this.userinfo, function(res) {
					if (res) {
						that.errMsg = "获取用户信息失败!";
					} else {
						//跳转具体界面
						that.dispatch();
					}
				})
			} else if (secret && secret != "null" && secret != "") {
				this.pub.setSecret(secret)
				//#ifdef H5
				//获取openid
				if (this.isWeiXin()) {
					this.getOpenidByJssDk(function(res) {
						if (res) {
							that.pub.getEncryptionOid(that.userinfo, function(res) {
								if (res) {
									that.errMsg = "获取用户信息失败!";
								} else {
									//跳转具体界面
									that.dispatch();
								}
							})
						}
					})
				}
				//#endif
			}
		},
		methods: {
			getOpenidByJssDk(callback) {
				callback = callback || function(res) {

				}
				// #ifdef H5
				let appid = this.pub.getAppid();
				let secret = this.pub.getSecret();
				if (!appid && !secret) {
					callback(false)
					return
				}
				let originUrl = window.location.href;
				let code = "";
				if (window.location.href.indexOf("code") != -1) {
					code = this.pub.getQueryString("code")
				}
				if (code == null || code == undefined || code == "") {
					let link =
						`https://open.weixin.qq.com/connect/oauth2/authorize?
					appid=${appid}&redirect_uri=${encodeURIComponent(originUrl)}&reurl=11&response_type=code&scope=${'snsapi_userinfo'}&state=321#wechat_redirect`
					window.location.href = link
				} else {
					let params = {
						code: code, //要去换取openid的登录凭证
						appid: appid,
						secret: secret
					}
					let _that = this
					this.$getOppenId(this.$path.WX_GET_OPENIDH5, params).then(data => {
						this.pub.setOpenid(data.data.openid)
						callback(true)
					}).fail(data => {
						callback(false)
					})
				}
				// #endif
			},
			isWeiXin() {
				// #ifdef H5
				var ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
				//#endif
				return false;
			},
			dispatch: function() {
				switch (page) {
					case "0": //首页
						uni.switchTab({
							url: "../homepage/homepage"
						})
						break;
					case "1": //景区列表
						{
							let location = null;
							let keyword = null;
							if (params) {
								location = params.location;
								keyword = params.keyword;
							}
							uni.redirectTo({
								url: "../scence/scencelist?city=" + location + "&keyword=" + keyword,
							})
						}
						break;
					case "2": //景区详情
						{
							let spotid = null;
							if (params) {
								spotid = params.spotid;
							}

							if (!spotid || spotid == "") {
								this.errMsg = "参数错误，未传spotid!";
								return
							}

							uni.redirectTo({
								url: "../scence/scencedetail?id=" + spotid
							})
						}
						break;
					case "3": //酒店列表
						{
							let location = null;
							let keyword = null;
							let inDate = null;
							let outDate = null;
							if (params) {
								location = params.location;
								keyword = params.keyword;
								inDate = params.inDate;
								outDate = params.outDate;
							}
							uni.redirectTo({
								url: "../hotel/hotellist?city=" + location + "&keyword=" + keyword + "&inDate=" + inDate + "&outDate=" +
									outDate,
							})
						}
						break;
					case "4": //酒店详情
						{
							let inDate = null;
							let outDate = null;
							let hotelid = null;
							if (params) {
								inDate = params.inDate;
								outDate = params.outDate;
								hotelid = params.hotelid;
							}
							if (!hotelid || hotelid == "") {
								this.errMsg = "参数错误，未传hotelid!";
								return
							}
							uni.redirectTo({
								url: "../hotel/hoteldetail?hotelid=" + hotelid + "&inDate=" + inDate + "&outDate=" +
									outDate,
							})
						}
						break;
					case "5": //订单列表
						{
							let type = null;
							if (params) {
								type = params.type;
							}
							uni.redirectTo({
								url: "../order/orderlist?type=" + type
							})
						}
						break;
				}
			},
		}
	}
</script>

<style>

</style>
